﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace TReInvoice
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }



        private void buttonCreateUbl_Click(object sender, EventArgs e)
        {

            ProfileIDType pId = new ProfileIDType();
            pId.Value = "TEMELFATURA";
            UBLVersionIDType ublVr = new UBLVersionIDType();
            ublVr.Value = "2.1";
            CustomizationIDType CustIdType = new CustomizationIDType();
            CustIdType.Value = "1.0";


            
            InvoiceType pInvoice = new InvoiceType();
            pInvoice.ProfileID = pId;
            pInvoice.UBLVersionID = ublVr;
            pInvoice.CustomizationID = CustIdType;
            
            var ilineL = new List<InvoiceLineType>();
            var iline = new InvoiceLineType();
            ItemType itype = new ItemType();
            var comtypeL = new List<CommodityClassificationType>();
            var comtype = new CommodityClassificationType();
            var ItemClass = new ItemClassificationCodeType();
            ItemClass.Value = "Ali Gel";
            comtype.ItemClassificationCode = ItemClass;
            comtypeL.Add(comtype);
            DescriptionType dectype = new DescriptionType();
            dectype.Value = "Deneme Ürünü";
            itype.Description = dectype;
            itype.CommodityClassification = comtypeL.ToArray();
            iline.Item = itype;
            ilineL.Add(iline);
            pInvoice.InvoiceLine = ilineL.ToArray();



            XmlWriterSettings wsettings = new XmlWriterSettings();
            wsettings.OmitXmlDeclaration = true;
            wsettings.Indent = true;
            XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(InvoiceType));


            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
            ns.Add("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
            ns.Add("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2");


            var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//SerializationOverview.xml";
            System.IO.FileStream file = System.IO.File.Create(path);
            XmlWriter xmlWriter = XmlWriter.Create(file, wsettings);

            writer.Serialize(xmlWriter, pInvoice, ns);

            xmlWriter.Close();
            file.Close();
            
        }
    }
}
